package uk.ac.ebi.bioinvindex.services;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import uk.ac.ebi.bioinvindex.dao.ejb3.OntologyEntryDAOHelper;
import uk.ac.ebi.bioinvindex.dao.ejb3.StudyEJB3DAO;
import uk.ac.ebi.bioinvindex.search.StudyFreeTextSearch;
import uk.ac.ebi.bioinvindex.model.impl.StudyImpl;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Oct 26, 2007
 */
@Name("studyBeanProvider")
@AutoCreate
public class StudyBeanProvider implements IStudyBeanProvider {

	@In
	private EntityManager entityManager;

	@In
	private StudyEJB3DAO studyEJB3DAO;

	@In
	private StudyFreeTextSearch studySearch;

	@In
	private OntologyEntryDAOHelper ontologyHelper;

	private String searchPattern;

	private String assayTypeFilterName;

	private String organism;

	public Collection<StudyBean> getItemsByRange(int firstRow, int numberOfRows) {
		List<StudyImpl> studies = fetchStudies();

		List<StudyBean> list = new ArrayList<StudyBean>(studies.size());
		int lastRow = studies.size();
		if (studies.size() > firstRow + numberOfRows) {
			lastRow = firstRow + numberOfRows;
		}
		for (int i = firstRow; i < lastRow; i++) {
			StudyImpl study = studies.get(i);
			StudyBeanImpl bean = new StudyBeanImpl(study);
			bean.setOntologyEntryDAOHelper(ontologyHelper);
			bean.setStudyDao(studyEJB3DAO);
			list.add(bean);
		}

		return list;
	}

	public int getRowCount() {
		return fetchStudies().size();
	}

	public StudyBean getStudyBeanById(Long id) {
		StudyImpl study = studyEJB3DAO.getById(id);
		StudyBeanImpl bean = new StudyBeanImpl(study);
		bean.setOntologyEntryDAOHelper(ontologyHelper);
		bean.setStudyDao(studyEJB3DAO);
		return bean;
	}

	public boolean hasStudyBeanById(Long id) {
		StudyImpl study = studyEJB3DAO.getById(id);
		return study != null;
	}

	/////////
	/// Bean gettes/setters
	/////
	public String getSearchPattern() {
		return searchPattern;
	}

	public void setSearchPattern(String searchPattern) {
		System.out.println("StudyBeanProvider.setSearchPattern " + searchPattern);
		this.searchPattern = searchPattern;
	}

	public String getAssayTypeFilterName() {
		System.out.println("StudyBeanProvider.getAssayTypeFilterName: " + assayTypeFilterName);
		return this.assayTypeFilterName;
	}

	public void setAssayTypeFilterName(String assayTypeFilterName) {
		System.out.println("StudyBeanProvider.setAssayTypeFilterName: " + assayTypeFilterName);
		this.assayTypeFilterName = assayTypeFilterName;
	}

	public String getOrganism() {
		return organism;
	}

	public void setOrganism(String organism) {
		this.organism = organism;
	}

	////////
	//private methods
	////
	private List<StudyImpl> fetchStudies() {
		List<StudyImpl> studies;

		//ToDo: integrate filtering by organism. Need to have references from Assay to characteristisc.
		if (searchPattern != null && searchPattern.length() > 0 &&
				assayTypeFilterName != null && assayTypeFilterName.length() > 0) {
			studies = studySearch.searchAndFilterByAssayType(searchPattern, assayTypeFilterName);
		} else if (searchPattern != null && searchPattern.length() > 0) {
			studies = studySearch.searchAllFields(searchPattern);
		} else if (assayTypeFilterName != null && assayTypeFilterName.length() > 0) {
			studies = studyEJB3DAO.findByAssayType(getAssayTypeFilterName());
		} else {
			studies = studyEJB3DAO.getAll();
		}
		return studies;
	}
}
